package com.yy.controller.service.Impl;

import com.yy.context.BaseContext;
import com.yy.controller.mapper.UserMapper;
import com.yy.controller.service.UserService;
import com.yy.entity.AdminUser;
import com.yy.entity.User;
import com.yy.exception.BaseException;
import com.yy.properties.JwtProperties;
import com.yy.utils.JwtUtil;
import com.yy.vo.AdminUserLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        User u = userMapper.getByPhone(user);
        //1.判断账号是否存在
        if (u == null) {
            //账号不存在 抛出异常
            throw new BaseException("账号不存在,请先注册!");
        }
        //2.判断密码是否正确
        String password = user.getPassword();
        //对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(u.getPassword())) {
            throw new BaseException("密码错误!");
        }
        //获取token
        Map<String, Object> clamins = new HashMap<>();
        clamins.put("UserId", u.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), clamins);
        u.setToken(token);
        return u;
    }

    /**
     * 验证码登录
     * @param user
     * @return
     */
    @Override
    public User yzmLogin(User user) {
        User u = userMapper.getByPhone(user);
        //1.判断账号是否存在
        if (u == null) {
            //账号不存在 抛出异常
            throw new BaseException("账号不存在,请先注册!");
        }
        //获取token
        Map<String, Object> clamins = new HashMap<>();
        clamins.put("UserId", u.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), clamins);
        u.setToken(token);
        return u;
    }
    /**
     * 修改密码
     *
     * @param user
     * @return
     */
    @Override
    public void update(User user) {
        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(password);
        Long id = BaseContext.getCurrentId();
        user.setId(id);
        userMapper.update(user);
    }

    /**
     * 注册账号
     *
     * @param user
     */
    @Override
    public void addUser(User user) {
        //查用户表
       //判断用户是否存在
        User u = userMapper.getByPhone(user);
        if (u != null) {
            //账号不存在 抛出异常
            throw new BaseException("账号已存在");
        }

        String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(password);
        Long id = BaseContext.getCurrentId();
        user.setId(id);
        user.setCreateTime(LocalDateTime.now());
        userMapper.add(user);
    }


}
